package org.example.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author Rommel
 * @version 1.0
 * @date 2023/6/19-13:12
 * @description TODO
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
    {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        //key序列号器设置为字符串
        template.setKeySerializer(new StringRedisSerializer());
        //value序列号器设置为fastJson
        template.setValueSerializer(new FastJsonRedisSerializer(Object.class));
        template.afterPropertiesSet();
        return template;
    }
}
